package c_lockproducer;
/*
    生产者线程
 */
public class Producer extends Thread{
    public Resource resource;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        //获取锁
        Resource.lock.lock();
        //flag = true,生产完毕,开始消费
        while (Resource.flag){
            try {
                //当前线程进入等待
                Resource.pro.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        Resource.count++;
        System.out.println("生产第" +Resource.count);
        //标记为生产完成
        Resource.flag = true;
        //唤醒另一个线程来进行消费
        Resource.cust.signal();
        //释放锁
        Resource.lock.unlock();
    }
}
